package com.yejhxt.Filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;

public class IndentifyFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		HttpSession session = req.getSession();
//		if ( StringUtils.contains(req.getRequestURL(), "/api")||req.getRemoteUser()!=null)
//			chain.doFilter(request, response);
		//if ( StringUtils.contains(req.getRequestURL(), "/api")||req.getRemoteUser()!=null)
		       if(StringUtils.endsWithAny(req.getRequestURL(), "/authorize.do","passwordUpdate.do","passwordUpdate.do","register.do","getValidateCode.do")
		        		  ||StringUtils.contains(req.getRequestURL(),"/sysmanager")||req.getRemoteUser()!=null)
			chain.doFilter(request, response);
		else {
			
			
			//System.out.println(session.getAttribute("User")+"      "+session.getAttribute("Role"));
			if (session.getAttribute("User") == null||session.getAttribute("Role")==null) {
				
				
//		Enumeration<String> str=	request.getParameterNames();
//		while(str.hasMoreElements()){
//			String f=str.nextElement();
//			System.out.println(f+"     "+req.getParameter(f) );
//			
//		}
//				
				String sid=request.getParameter("access_token");
          //System.out.println(sid);
				if(!StringUtils.isBlank(sid))
				{
			//		   System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");	
					session = ProjectTools.getAppSession(sid);
					if(session!=null)
					{
				//		System.out.println("bbbbbbbbbbbb");
						if (session.getAttribute("User") != null||session.getAttribute("Role")!=null) {
							chain.doFilter(request, response);
							
						}
						
					}
					
				}
				
				
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("error", "invalid_request");
			map.put("status", "403");

			map.put("error_description", "Wrong username or password");
			
			String j = new Gson().toJson(map);
		
			ProjectTools.ResponseOut(resp, j);
		} else
			chain.doFilter(request, response);
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		

		
	}

}
